package Homework;

import java.time.LocalDate;
import java.util.Objects;

public class Commodity implements Comparable<Commodity>{
    /*
    创建商品类，定义属性：编号、商品名、单价、厂家、生产日期。按标准javaBean创建。
创建业务类，完成商品集合算法的封装。

按要求定义实体类、接口、以及业务类。定义测试类，测试业务类中方法的正确性。
     */
    private int code;
    private String name;
    private String price;
    private String factory;
    private LocalDate date;

    public Commodity(int code, String name, String price, String factory, String date) {
        this.code = code;
        this.name = name;
        this.price = price;
        this.factory = factory;
        this.date = LocalDate.parse(date);
    }

    public Commodity(int code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getFactory() {
        return factory;
    }

    public void setFactory(String factory) {
        this.factory = factory;
    }

    public LocalDate getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = LocalDate.parse(date);
    }

    @Override
    public String toString() {
        return "Commodity{" +
                "code='" + code + '\'' +
                ", name='" + name + '\'' +
                ", price='" + price + '\'' +
                ", factory='" + factory + '\'' +
                ", date='" + date + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Commodity commodity = (Commodity) o;
        return code == commodity.code;
    }

    @Override
    public int hashCode() {
        return Objects.hash(code);
    }

    @Override
    public int compareTo(Commodity o) {
        return Integer.parseInt(o.getPrice())-Integer.parseInt(this.getPrice());
    }
}
